﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.StaticData;
using WmsApp.StaticData.Unity;

namespace WmsApp.UI
{
    public partial class QInStoreBill : DevExpress.XtraEditors.XtraUserControl, IQueryControl
    {
        private BaseGoodsCodeTypeObserver goodsCodeTypeObserver = null;
        public QInStoreBill()
        {
            InitializeComponent();
            if (GlobalClass.RunTimeMode)
            {
                DataBinding.Binding.BindingBillState(this.lookUpEditBillState);
                DataBinding.Binding.BindingGoodsCodeType(this.lookUpEditGoodsCodeType);
                DataBinding.Binding.BindInStoreType(this.lookUpEditInStoreType);
                DataBinding.Binding.BindProvider(this.lookUpEditProvider);
                DataBinding.Binding.BindStoreType(this.lookUpEditStoreType);
                DataBinding.Binding.BindUnit(this.lookUpEditUnit);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (GlobalClass.RunTimeMode)
            {
                SubjectFactory.AddObserverToSubject(new CustomerObserver("customer", SubjectFactory.GetSubject(),
                                                                         this.lookUpEditProvider.Properties,
                                                                         (ICanClosing) this.FindForm()));

                var subject = StaticData.GoodsCodeTypeSubjectFactory.GetGoodsCodeTypeSubject();
                goodsCodeTypeObserver = new GoodsCodeTypeObserver("in", subject,
                                                                 lookUpEditGoodsCodeType, this.ParentForm);
                subject.Attach(goodsCodeTypeObserver);
            }
        }

        #region IQueryControl Members

        public  string GetCondition()
        {
            string condition = "";
            if (lookUpEditBillState.EditValue != null && !String.IsNullOrEmpty(lookUpEditBillState.EditValue.ToString()))
            {
                condition += (String.Format(" and BillState ='{0}'", lookUpEditBillState.EditValue));
            }
            if (lookUpEditGoodsCodeType.EditValue != null && !String.IsNullOrEmpty(lookUpEditGoodsCodeType.EditValue.ToString()))
            {
                condition += (String.Format(" and BillID in ( select BillID from dbo.InStoreBillDtl where  NewGoodsCode ='{0}' )", lookUpEditGoodsCodeType.EditValue));
            }
            if (lookUpEditInStoreType.EditValue != null && !String.IsNullOrEmpty(lookUpEditInStoreType.EditValue.ToString()))
            {
                condition += (String.Format(" and ExtVarChar1 ='{0}' ", lookUpEditInStoreType.EditValue));
            }
            if (lookUpEditProvider.EditValue != null && !String.IsNullOrEmpty(lookUpEditProvider.EditValue.ToString()))
            {
                condition += (String.Format(" and ProviderCode ='{0}'", lookUpEditProvider.EditValue));
            }
            if (lookUpEditStoreType.EditValue != null && !String.IsNullOrEmpty(lookUpEditStoreType.EditValue.ToString()))
            {
                condition += (String.Format(" and BillID in ( select BillID from dbo.InStoreBillDtl where  StoreTypeCode ='{0}' )", lookUpEditStoreType.EditValue));
            }
            if (lookUpEditUnit.EditValue != null && !String.IsNullOrEmpty(lookUpEditUnit.EditValue.ToString()))
            {
                condition += (String.Format(" and BillID in ( select BillID from dbo.InStoreBillDtl where  UnitCode ='{0}' )", lookUpEditUnit.EditValue));
            }

            if(!String.IsNullOrEmpty(this.textEditInStoreBIllID.Text))
            {
                condition += (String.Format(" and BillID  like '%{0}%'", textEditInStoreBIllID.Text));
            }

            if(this.dateEditBillDateFrom.EditValue != null)
            {
                condition += (String.Format(" and InBillDate  >= '{0}'", dateEditBillDateFrom.Text));
            }

            if (this.dateEditBillDateTo.EditValue != null)
            {
                condition += (String.Format(" and InBillDate  <= '{0}'", dateEditBillDateTo.Text));
            }

            if (!String.IsNullOrEmpty(textEditNewGoodsName.Text.Trim()))
            {
                condition += (String.Format(" and BillID in ( select BillID from dbo.InStoreBillDtl where  NewGoodsName  like '%{0}%' )", textEditNewGoodsName.Text.Trim()));
            }

            if (!String.IsNullOrEmpty(this.customCondition))
            {
                condition += customCondition;
            }
            return condition;
        }

        #endregion

        #region IQueryControl Members

        protected string customCondition = "";
        public virtual void AddCustomerCondition(string condition)
        {
            customCondition = condition;
        }

        #endregion
    }
}
